using Brouteforce.NewManagers;
using Brouteforce.UserInterface.Abstracts;
using Brouteforce.UserInterface.Interfaces;
using Brouteforce.UserInterface.UIControls;
using Brouteforce.UserInterface.UIControls.MenuItems;
using Brouteforce.utilities;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Brouteforce.Screens.MenuScreen.Panels {
    public class MenuSelectionPanel : AbstractPanel {
        private int currentUIControlIndex = 0;

        public MenuSelectionPanel()
            : base("MenuSelectionPanel") {

            //todo - when we know which one is the longest word to change the 0 param to the length of the biggest word and the second to the biggest ...
            var screenBounds = CalculteCenteredRectangle(Bounds, 0, (int)Draws.NormalFontSize.Y * 2);
            var MenuItemName = "Start Game";
            var diffBetweenLines = Draws.CalculateWordSizeNormalFont(MenuItemName);
            AbstractMenuItem abstractMenuItem = new StartGameAbstractMenuItem(MenuItemName, new Rectangle(screenBounds.X, screenBounds.Y, (int)diffBetweenLines.X, (int)diffBetweenLines.Y));
            AddUIControl(abstractMenuItem);

            //sets the "start game" menu item as the focused one
            UIControlManager.Instance.ChangeFocousedUIControl(abstractMenuItem);

            MenuItemName = "About";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(MenuItemName);
            abstractMenuItem = new AboutAbstractMenuItem(MenuItemName,
                                    new Rectangle(screenBounds.X, screenBounds.Y + (int)diffBetweenLines.Y, (int)diffBetweenLines.X,
                                                  (int)diffBetweenLines.Y));
            AddUIControl(abstractMenuItem);
        }

        public override void Update(GameTime gameTime) {
            foreach (AbstractUIControl uiControl in Children)
                uiControl.Update();
            //todo if on panel
            //if (InputManager.IsMouseClickedOn(PanelRectangle)) 

            //UIControlManager.ChangeFocousedUIControl(_uiControls[currentUIControlIndex]);
            foreach (var key in InputManager.CurrentClickedKeys) {
                switch (key) {
                    case Keys.Tab:
                        var focusedUIcontrolIndex = Children.IndexOf(UIControlManager.Instance.CurrentFocuseOn);
                        var nextWantedFocusedUIcontrol = Children[(focusedUIcontrolIndex + 1) % Children.Count];
                        UIControlManager.Instance.ChangeFocousedUIControl(nextWantedFocusedUIcontrol);
                        break;
                }
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) {
            //to add location by Enum
            foreach (var UIcontrol in Children) {
                UIcontrol.Draw(gameTime);
            }
        }

        public override void AfterLayout()
        {
            
        }
    }
}
